#!/usr/bin/python
# -*- coding:utf-8 -*-

import MySQLdb


def insert_off_shelve(off_shelve):

    db = MySQLdb.connect("127.0.0.1", "root", "123", "openDB")
    cursor = db.cursor()
    with open("./items.txt", "r") as f:
        items = f.readlines()
    for item in items:
        item = item.split('\n')[0]
        try:
            sql = "insert into " + off_shelve + "(product_id, add_time) values ('" + item + "', now())"
            cursor.execute(sql)
            db.commit()
        except Exception, e:
            print str(e)
            db.rollback()
            continue
    db.close()


if __name__ == '__main__':
    insert_off_shelve('off_shelve_1')
